package net.suncaper.hotel.service.impl;

import net.suncaper.hotel.common.utils.PageInfo;
import net.suncaper.hotel.mapper.IRoomMapper;
import net.suncaper.hotel.mapper.IRoomTypeMapper;
import net.suncaper.hotel.mapper.impl.RoomMapperImpl;
import net.suncaper.hotel.mapper.impl.RoomTypeMapperImpl;
import net.suncaper.hotel.model.domain.Room;
import net.suncaper.hotel.model.domain.RoomType;
import net.suncaper.hotel.model.dto.RoomDTO;
import net.suncaper.hotel.model.dto.RoomTypeDTO;
import net.suncaper.hotel.model.vo.RoomTypeVO;
import net.suncaper.hotel.model.vo.RoomVO;
import net.suncaper.hotel.service.IRoomService;

import java.util.ArrayList;
import java.util.List;

/**
 * 房间服务实现类
 */
public class RoomServiceImpl implements IRoomService {
    private IRoomMapper roomMapper = new RoomMapperImpl();
    private IRoomTypeMapper roomTypeMapper = new RoomTypeMapperImpl();

    @Override
    public List<RoomTypeVO> getRoomTypeList() {
        List<RoomType> types = roomTypeMapper.findAll();
        List<RoomTypeVO> voList = new ArrayList<>();
        
        if (types != null) {
            for (RoomType type : types) {
                RoomTypeVO vo = convertToRoomTypeVO(type);
                // 统计该类型的房间数量
                vo.setRoomCount((int) roomMapper.countByRoomTypeId(type.getId()));
                vo.setAvailableCount((int) roomMapper.findByRoomTypeId(type.getId()).stream()
                    .filter(r -> "AVAILABLE".equals(r.getStatus())).count());
                voList.add(vo);
            }
        }
        
        return voList;
    }

    @Override
    public boolean createRoomType(RoomTypeDTO dto) {
        if (dto == null || dto.getTypeName() == null) {
            return false;
        }
        
        RoomType roomType = new RoomType();
        roomType.setTypeName(dto.getTypeName());
        roomType.setBedType(dto.getBedType());
        roomType.setMaxOccupancy(dto.getMaxOccupancy());
        roomType.setRoomSize(dto.getRoomSize());
        roomType.setPrice(dto.getPrice());
        roomType.setDescription(dto.getDescription());
        roomType.setAmenities(dto.getAmenities());
        roomType.setStatus(dto.getStatus() != null ? dto.getStatus() : 1);
        
        return roomTypeMapper.insert(roomType) > 0;
    }

    @Override
    public boolean updateRoomType(RoomTypeDTO dto) {
        if (dto == null || dto.getId() == null) {
            return false;
        }
        
        RoomType roomType = roomTypeMapper.findById(dto.getId());
        if (roomType == null) {
            return false;
        }
        
        roomType.setTypeName(dto.getTypeName());
        roomType.setBedType(dto.getBedType());
        roomType.setMaxOccupancy(dto.getMaxOccupancy());
        roomType.setRoomSize(dto.getRoomSize());
        roomType.setPrice(dto.getPrice());
        roomType.setDescription(dto.getDescription());
        roomType.setAmenities(dto.getAmenities());
        roomType.setStatus(dto.getStatus());
        
        return roomTypeMapper.update(roomType) > 0;
    }

    @Override
    public boolean deleteRoomType(Integer id) {
        if (id == null) {
            return false;
        }
        
        // 检查是否有房间使用该类型
        long count = roomMapper.countByRoomTypeId(id);
        if (count > 0) {
            return false; // 还有房间使用该类型，不能删除
        }
        
        return roomTypeMapper.delete(id) > 0;
    }

    @Override
    public PageInfo<RoomVO> getRoomList(int pageNum, int pageSize) {
        PageInfo<RoomVO> pageInfo = new PageInfo<>(pageNum, pageSize);
        
        // 获取总数
        long total = roomMapper.count();
        pageInfo.setTotal(total);
        
        // 获取分页数据
        List<Room> rooms = roomMapper.findByPage(pageInfo.getStartRow(), pageSize);
        List<RoomVO> voList = new ArrayList<>();
        if (rooms != null) {
            for (Room room : rooms) {
                voList.add(convertToRoomVO(room));
            }
        }
        pageInfo.setList(voList);
        
        return pageInfo;
    }

    @Override
    public List<RoomVO> getRoomsByStatus(String status) {
        List<Room> rooms = roomMapper.findByStatus(status);
        List<RoomVO> voList = new ArrayList<>();
        
        if (rooms != null) {
            for (Room room : rooms) {
                voList.add(convertToRoomVO(room));
            }
        }
        
        return voList;
    }

    @Override
    public List<RoomVO> getRoomsByType(Integer roomTypeId) {
        List<Room> rooms = roomMapper.findByRoomTypeId(roomTypeId);
        List<RoomVO> voList = new ArrayList<>();
        
        if (rooms != null) {
            for (Room room : rooms) {
                voList.add(convertToRoomVO(room));
            }
        }
        
        return voList;
    }

    @Override
    public RoomVO getRoomById(Integer id) {
        if (id == null) {
            return null;
        }
        Room room = roomMapper.findById(id);
        return convertToRoomVO(room);
    }

    @Override
    public boolean createRoom(RoomDTO dto) {
        if (dto == null || dto.getRoomNumber() == null) {
            return false;
        }
        
        // 检查房间号是否已存在
        Room existRoom = roomMapper.findByRoomNumber(dto.getRoomNumber());
        if (existRoom != null) {
            return false;
        }
        
        Room room = new Room();
        room.setRoomNumber(dto.getRoomNumber());
        room.setRoomTypeId(dto.getRoomTypeId());
        room.setFloor(dto.getFloor());
        room.setStatus(dto.getStatus() != null ? dto.getStatus() : "AVAILABLE");
        room.setNotes(dto.getNotes());
        
        return roomMapper.insert(room) > 0;
    }

    @Override
    public boolean updateRoom(RoomDTO dto) {
        if (dto == null || dto.getId() == null) {
            return false;
        }
        
        Room room = roomMapper.findById(dto.getId());
        if (room == null) {
            return false;
        }
        
        // 如果修改了房间号，检查新房间号是否已存在
        if (!room.getRoomNumber().equals(dto.getRoomNumber())) {
            Room existRoom = roomMapper.findByRoomNumber(dto.getRoomNumber());
            if (existRoom != null) {
                return false;
            }
        }
        
        room.setRoomNumber(dto.getRoomNumber());
        room.setRoomTypeId(dto.getRoomTypeId());
        room.setFloor(dto.getFloor());
        room.setStatus(dto.getStatus());
        room.setNotes(dto.getNotes());
        
        return roomMapper.update(room) > 0;
    }

    @Override
    public boolean updateRoomStatus(Integer id, String status) {
        if (id == null || status == null) {
            return false;
        }
        return roomMapper.updateStatus(id, status) > 0;
    }

    @Override
    public boolean deleteRoom(Integer id) {
        if (id == null) {
            return false;
        }
        
        // TODO: 检查房间是否有预订或入住记录
        
        return roomMapper.delete(id) > 0;
    }

    private RoomTypeVO convertToRoomTypeVO(RoomType roomType) {
        if (roomType == null) {
            return null;
        }
        
        RoomTypeVO vo = new RoomTypeVO();
        vo.setId(roomType.getId());
        vo.setTypeName(roomType.getTypeName());
        vo.setBedType(roomType.getBedType());
        vo.setMaxOccupancy(roomType.getMaxOccupancy());
        vo.setRoomSize(roomType.getRoomSize());
        vo.setPrice(roomType.getPrice());
        vo.setDescription(roomType.getDescription());
        vo.setAmenities(roomType.getAmenities());
        vo.setStatus(roomType.getStatus());
        vo.setCreateTime(roomType.getCreateTime());
        
        return vo;
    }

    private RoomVO convertToRoomVO(Room room) {
        if (room == null) {
            return null;
        }
        
        RoomVO vo = new RoomVO();
        vo.setId(room.getId());
        vo.setRoomNumber(room.getRoomNumber());
        vo.setRoomTypeId(room.getRoomTypeId());
        vo.setFloor(room.getFloor());
        vo.setStatus(room.getStatus());
        vo.setNotes(room.getNotes());
        vo.setCreateTime(room.getCreateTime());
        vo.setUpdateTime(room.getUpdateTime());
        
        // 获取房型信息
        RoomType roomType = roomTypeMapper.findById(room.getRoomTypeId());
        if (roomType != null) {
            vo.setTypeName(roomType.getTypeName());
            vo.setBedType(roomType.getBedType());
            vo.setPrice(roomType.getPrice());
        }
        
        // 设置状态文本
        switch (room.getStatus()) {
            case "AVAILABLE":
                vo.setStatusText("可用");
                break;
            case "OCCUPIED":
                vo.setStatusText("已入住");
                break;
            case "CLEANING":
                vo.setStatusText("清洁中");
                break;
            case "MAINTENANCE":
                vo.setStatusText("维修中");
                break;
            default:
                vo.setStatusText(room.getStatus());
        }
        
        return vo;
    }
}